package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.PDealerSchool;
import com.ruoyi.system.domain.vo.PSchoolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.PSchool;
import com.ruoyi.system.service.IPSchoolService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学校Controller
 * 
 * @author ruoyi
 * @date 2024-01-28
 */
@RestController
@RequestMapping("/school")
public class PSchoolController extends BaseController
{
    @Autowired
    private IPSchoolService pSchoolService;

    /**
     * 查询学校列表
     */
    @RequiresPermissions("system:school:list")
    @GetMapping("/list")
    public TableDataInfo list(PSchool pSchool)
    {
        startPage();
        List<PSchoolVo> list = pSchoolService.selectPSchoolList(pSchool);
        return getDataTable(list);
    }

    /**
     * 导出学校列表
     */
    @RequiresPermissions("system:school:export")
    @Log(title = "学校", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PSchool pSchool)
    {
        List<PSchoolVo> list = pSchoolService.selectPSchoolList(pSchool);
        ExcelUtil<PSchoolVo> util = new ExcelUtil<PSchoolVo>(PSchoolVo.class);
        util.exportExcel(response, list, "学校数据");
    }

    /**
     * 获取学校详细信息
     */
    @RequiresPermissions("system:school:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pSchoolService.selectPSchoolById(id));
    }

    /**
     * 新增学校
     */
    @RequiresPermissions("system:school:add")
    @Log(title = "学校", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PSchoolVo pSchool)
    {
        try {
            return toAjax(pSchoolService.insertPSchool(pSchool));
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 分配分销商
     */
    @RequiresPermissions("system:school:signPSchool")
    @Log(title = "学校", businessType = BusinessType.INSERT)
    @PostMapping(value = "signPSchool")
    public AjaxResult signPSchool(@RequestBody PDealerSchool pDealerSchool)
    {
        try {
            return toAjax(pSchoolService.signPSchool(pDealerSchool));
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 修改学校
     */
    @RequiresPermissions("system:school:edit")
    @Log(title = "学校", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PSchoolVo pSchool) throws Exception {
        return toAjax(pSchoolService.updatePSchool(pSchool));
    }

    /**
     * 删除学校
     */
    @RequiresPermissions("system:school:remove")
    @Log(title = "学校", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pSchoolService.deletePSchoolByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<PSchoolVo> util = new ExcelUtil<PSchoolVo>(PSchoolVo.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @Log(title = "学校", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:school:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PSchoolVo> util = new ExcelUtil<PSchoolVo>(PSchoolVo.class);
        List<PSchoolVo> schoolVoList = util.importExcel(file.getInputStream());
        String message = pSchoolService.importSchool(schoolVoList, updateSupport);
        return success(message);
    }
}
